package exception_learn;/**
 * @author yuisama
 * @date 2022/09/25 11:32
 **/
public class UserLogin {
    private String userName = "admin";
    private String password = "123456";

    public static void main(String[] args) {
        UserLogin userLogin = new UserLogin();
        try {
            userLogin.login("admin","1234567");
        } catch (PasswordException e) {
            e.printStackTrace();
        }
    }

    public void login(String userName,String password) throws PasswordException{
        if (!this.userName.equals(userName)) {
            throw new UserNameException("用户名错误!");
        }
        if (!this.password.equals(password)) {
            // 产生异常对象之后需要显示处理
            // 显示处理有两种，要么在当前方法try。。catch
            // 要么通过throws向上抛出
            throw new PasswordException("密码错误！");
        }
    }

}
// 密码异常，受查异常
class PasswordException extends Exception {
    public PasswordException(String msg) {
        super(msg);
    }
}
// 用户名异常,非受查异常
class UserNameException extends RuntimeException {
    public UserNameException(String msg) {
        super(msg);
    }
}


